<template>
	<view class="page">
		<u-navbar title="我的计划" border @leftClick="$utils.back()" placeholder />
		<view class="filterBox flex">
			<uni-data-select placeholder='请选择年份' v-model="year" :localdata="range" @change="$refs.paging.reload()"></uni-data-select>
			<view class="flex">
				<view @click="clickBut(1)" class="but" :class="{choose:status==1}">
					未完成
				</view>
				<view @click="clickBut(2)" class="but ml20" :class="{choose:status==2}">
					完成
				</view>
			</view>
		</view>
		<view class="body">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="10">
				<view class="contentBox mt20 flex" v-for="item in data" :key="item.id" @click="jump(item.id)">
					<image :src='item.thumb' mode="aspectFill" />
					<view class="ml20 flex" style="align-items: flex-start;">
						<view class="flex" style=" width: 100%;">
							<view class="flex" style="flex: 1;">
								<view>
									<u-text lines='1' :text='item.user.nick_name' size='28rpx' color='#999' />
								</view>
								<view class="but ml20" style="min-width: 112rpx;box-sizing: border-box;" v-if="item.status==1">
									执行中
								</view>
							</view>
							<view class="ml20">
								<u-icon name="more-dot-fill" size="50rpx" @click.stop='openSelect(item)'></u-icon>
							</view>
						</view>
						<view>
							<u-text lines='1' :text='item.title' size='24rpx' />
						</view>
						<view>
							开始:{{item.start_at}}
						</view>
					</view>
				</view>
			</z-paging>
		</view>
		<view class="add viewWidth" @click="jumpAdd" v-if="!loading">新增计划
		</view>
		<root-portal>
		
			<u-action-sheet :actions="columns" :show="selectShow" @close='selectShow=false' @select="confirm"
				closeOnClickOverlay></u-action-sheet>
			<u-modal :show="delShow" showCancelButton @confirm='del()' @cancel='delShow=false'>
				<view style="padding: 12px 25px 25px 25px;font-size: 30rpx;">
					是否确认删除
				</view>
			</u-modal>
		</root-portal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				delShow:false,
				selectItem: {},
				selectShow: false,
				user: uni.getStorageSync('user'),
				disabled: false,
				status: null,
				year: null,
				range: [],
				data: [],
				loading: true,
			}
		},
		onLoad() {
			let currentYear = new Date().getFullYear(); // 获取当前年份
			this.year=currentYear
			let startYear = currentYear - 20; // 计算起始年份
			let endYear = currentYear; // 结束年份就是当前年份
			for (let year = startYear; year <= endYear; year++) {
				this.range.push({
					value: year,
					text: year
				});
			}
		},
		computed: {
			columns() {
				return [{
						name: '编辑'
					},
					{
						name: '删除'
					},
					{
						name: '退出'
					},
				]
			}
		},
		watch: {
			status() {
				this.$refs.paging.reload()
			}
		},
		methods: {
			jump(id){
				this.$utils.navigateTo({
					url:'/plan/detail?id='+id
				})
			},
			del() {
				this.delShow = false
				if (this.disabled) return
				this.disabled = true
				uni.showLoading({
					title: '删除中...'
				})
				this.$api.Plan.Del(this.selectItem.id).then(err => {
					this.data = this.data.filter(item => item.id != this.selectItem.id)
					this.disabled = false
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.data.message)
					this.disabled = false
				})
			},
			confirm(e) {
				this.selectShow = false
				if (e.name == '编辑') {
					this.$utils.navigateTo({
						url: '/plan/update?id=' + this.selectItem.id
					})
				} else if (e.name == '删除') {
					this.delShow = true
				} 
			},
			openSelect(item) {
				this.selectShow = true
				this.selectItem = item
			},
			clickBut(e) {
				if (this.status == e) {
					this.status = null
					return
				}
				this.status = e
			},
			jumpAdd() {
				this.$utils.navigateTo({
					url: '/plan/add'
				})
			},
			query(page, size) {
				let obj = {
					page,
					size
				}
				if (this.status) {
					obj.status = this.status
				}
				if (this.year) {
					obj.year = this.year
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api.My.Plan(obj).then(res => {
					uni.hideLoading()
					this.$refs.paging.complete(res.data.data.map(item => {
						return {
							...item,
							user: this.user
						}
					}))
					this.loading = false
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.data.message)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .uni-select{
		border: none !important;
	}
	.add {
		border-radius: 100rpx;
		background-color: #ddd;
		text-align: center;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.page {
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.body {
		flex: 1;
		overflow: hidden;
	}

	.filterBox {
		justify-content: space-between;
		margin-top: 20rpx;
	}

	.flex {
		display: flex;
		align-items: center;
	}

	.but {
		border-radius: 100rpx;
		border: 1rpx solid #bbb;
		padding: 5rpx 20rpx;
	}

	.choose {
		color: #fff;
		border: none;
		background-color: #3975c6;
	}




	.contentBox {
		image {
			width: 150rpx;
			height: 150rpx;
			border-radius: 8rpx;
		}

		>view:nth-child(2) {
			flex-direction: column;
			justify-content: space-between;
			flex: 1;
			height: 150rpx;
		}
	}
</style>